import java.util.Arrays;
import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int len1 = sc.nextInt();
        int len2 = sc.nextInt();
        int[][] nums = new int[len1][len2];
        sc.nextLine();
        for (int j = 0; j < len1; ++j) {
            String[] s = sc.nextLine().trim().split(" ");

            for (int i = 0; i < len2; ++i) {
                nums[j][i] = Integer.parseInt(s[i]);
            }
        }
        System.out.println(Arrays.toString(spiralOrder(nums)));

    }
    public static int[] spiralOrder(int[][] matrix) {
        /*涉及到二维数组一定要进行判断*/
        if (matrix.length == 0 ) {
            return new int[0];
        }

        int len1 = matrix.length;
        int len2 = matrix[0].length;

        int top = len2;
        int bottom = len2 - 1;
        int rightcolumn = len1 - 1;
        int leftcolumn = len1 - 2;
        int i = 0, j = -1;
        int[] res = new int[len1 * len2];
        int n = 0;
        while (true) {
            for (int k = 0; k < top; k++) {
                res[n++] = matrix[i][++j];
            }
            if (top <= 0) {break;}

            for (int k = 0; k < rightcolumn; k++) {
                res[n++] = matrix[++i][j];
            }
            if (rightcolumn <= 0) {break;}

            for (int k = 0; k < bottom; k++) {
                res[n++] = matrix[i][--j];
            }
            if (bottom <= 0) {break;}

            for (int k = 0; k < leftcolumn; k++) {
                res[n++] = matrix[--i][j];
            }
            if (leftcolumn <= 0) {break;}

            top -= 2;
            rightcolumn -= 2;
            bottom -= 2;
            leftcolumn -= 2;
        }
        return res;

    }
}

